package com.yc.txx.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yc.txx.entity.Car;
import com.yc.txx.entity.Lease;
import com.yc.txx.mapper.CarMapper;
import com.yc.txx.mapper.LeaseMapper;
import com.yc.txx.service.BizException;
import com.yc.txx.service.ILeaseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yc.txx.util.Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 天下行租车-用车表-10 服务实现类
 * </p>
 *
 * @author xl
 * @since 2024-05-07
 */
@Service
public class LeaseServiceImpl extends ServiceImpl<LeaseMapper, Lease> implements ILeaseService {

    @Autowired
    CarMapper carMapper;

    @Autowired
    LeaseMapper leaseMapper;

    @Transactional
    @Override
    public void commit(Lease lease) throws BizException {
        Utils.check(lease.getDays().split(",").length==0,
                "请选择租车日期");
        Utils.check(Utils.isEmpty(lease.getUid()),
                "请先登录系统");

        Car car = carMapper.selectById(lease.getCid());

        lease.setState("提车");
        lease.setOrderTime(LocalDateTime.now());
        lease.setMoney(car.getPrice().longValue() *
                lease.getDays().split(",").length );

        car.setState("出库");
        carMapper.updateById(car);
        save(lease);

    }


    @Override
    public List<Lease> queryLease(Lease lease, IPage<Lease> page) throws BizException {
        Utils.check(Utils.isEmpty(lease.getUid()),"请先登录系统");
        return leaseMapper.queryLease(lease.getUid(),page);
    }
}
